/*
 * Copyright kecp
 * Jun 02, 2010
 */

package Gui;

import LBS.GoogleMap;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import traveller.Assistant;

/**
 * 显示地图信息
 * @author kecp
 */
public class MapView implements CommandListener {

    private MIDlet midlet;
    private Assistant assist;
    Image mapImg;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form mapForm;
    private Command updateLocation;
    private Command backCommand;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The MapView constructor.
     * @param midlet the midlet used for getting
     */
    public MapView (MIDlet midlet) {
        this.midlet = midlet;
        assist = Assistant.getTheAssistant();
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|2-switchDisplayable|0|2-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|2-switchDisplayable|0|2-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|2-switchDisplayable|1|2-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|2-switchDisplayable|1|2-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|2-switchDisplayable|2|
    //</editor-fold>//GEN-END:|2-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mapForm ">//GEN-BEGIN:|11-getter|0|11-preInit
    /**
     * Returns an initiliazed instance of mapForm component.
     * @return the initialized component instance
     */
    public Form getMapForm() {
        if (mapForm == null) {//GEN-END:|11-getter|0|11-preInit
            // write pre-init user code here
            mapForm = new Form("map");//GEN-BEGIN:|11-getter|1|11-postInit
            mapForm.addCommand(getUpdateLocation());
            mapForm.addCommand(getBackCommand());
            mapForm.setCommandListener(this);//GEN-END:|11-getter|1|11-postInit
            // write post-init user code here
        }//GEN-BEGIN:|11-getter|2|
        return mapForm;
    }
    //</editor-fold>//GEN-END:|11-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|4-commandAction|0|4-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|4-commandAction|0|4-preCommandAction
        // write pre-action user code here
        if (displayable == mapForm) {//GEN-BEGIN:|4-commandAction|1|19-preAction
            if (command == backCommand) {//GEN-END:|4-commandAction|1|19-preAction
                // write pre-action user code here
                ((MainMIDlet)midlet).back();
//GEN-LINE:|4-commandAction|2|19-postAction
                // write post-action user code here
            } else if (command == updateLocation) {//GEN-LINE:|4-commandAction|3|17-preAction
                // write pre-action user code here
                updateMap();
//GEN-LINE:|4-commandAction|4|17-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|5|4-postCommandAction
        }//GEN-END:|4-commandAction|5|4-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|4-commandAction|6|
    //</editor-fold>//GEN-END:|4-commandAction|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: updateLocation ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of updateLocation component.
     * @return the initialized component instance
     */
    public Command getUpdateLocation() {
        if (updateLocation == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            updateLocation = new Command("\u6211\u7684\u4F4D\u7F6E", Command.SCREEN, 0);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return updateLocation;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            backCommand = new Command("\u540E\u9000", Command.BACK, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(midlet);
    }

    /**
     * Exits MIDlet.
     * Note you have to implement proper MIDlet destroying.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        // midlet.destroyApp(true);
        midlet.notifyDestroyed();
    }

    protected void updateMap() {
        mapImg = assist.getCurMap();
        mapForm.append(mapImg);
    }

}
